package com.dmai.capacity.platform.oauth.common.service.impl;

import com.dmai.capacity.platform.common.lock.DistributedLock;
import com.dmai.capacity.platform.common.lock.LockAdapter;
import com.dmai.capacity.platform.common.model.CapabilityPackage;
import com.dmai.capacity.platform.common.model.CapabilityPackageDeductionRule;
import com.dmai.capacity.platform.common.model.OauthUserPackage;
import com.dmai.capacity.platform.oauth.common.mapper.CapabilityPackageDeductionRuleDao;
import com.dmai.capacity.platform.oauth.common.mapper.OauthUserPackageDao;
import com.dmai.capacity.platform.oauth.common.mapper.CapabilityPackageDao;
import com.dmai.capacity.platform.oauth.common.constants.ResourcePackageTypeEnum;
import com.dmai.capacity.platform.oauth.common.service.ICapabilityPackageService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class CapabilityPackageServiceImpl implements ICapabilityPackageService {

    @Autowired
    private CapabilityPackageDao packageDao;

    @Autowired
    private OauthUserPackageDao userPackageDao;

    @Autowired
    private CapabilityPackageDeductionRuleDao ruleDao;

    @Autowired
    private DistributedLock locker;

    /**
     * 添加
     *
     * @param rp
     */
    @Override
    public int save(CapabilityPackage rp) {
        return packageDao.save(rp);
    }

    /**
     * 修改
     *
     * @param rp
     */
    @Override
    public int update(CapabilityPackage rp) {
        return packageDao.update(rp);
    }


    /**
     * 删除
     *
     * @param id
     */
    @Override
    public int delete(Integer id) {
        return packageDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    @Override
    public CapabilityPackage get(Integer id) {
        return packageDao.findById(id);
    }


    @Override
    @SneakyThrows
    public Boolean deduction(Integer capabilityId, Integer packageId) {
        try (LockAdapter lockAdapter = locker.lock("update_resource_package_" + packageId);) {
            OauthUserPackage model = userPackageDao.findById(packageId);

            int _deduction = 1;
            if (model.getType().equals(ResourcePackageTypeEnum.SHARE.getCode())) {
                // 共享资源包
                CapabilityPackageDeductionRule rule = ruleDao.find(capabilityId);
                _deduction = rule.getCount();
            }
            if (model.getQuotaAvailable() >= _deduction) {
                model.setQuotaAvailable(model.getQuotaAvailable() - _deduction);
                if (model.getQuotaAvailable() <= 0) {
                    model.setEffective(0);
                }
            } else {
                // 不够扣除扣一次，资源包失效
                model.setQuotaAvailable(0);
                model.setEffective(0);
            }

            if (model.getExpiredTime().compareTo(new Date()) <= 0) {
                model.setEffective(0);
            }
            return userPackageDao.updateAvailable(model) > 0;
        }
    }
}
